/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  XESConvertorMonitor.java                                      
 *  File Contents Outline :  XES转换监视器
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
package cn.edu.thu.log.xes;

import java.util.Date;
import java.util.Hashtable;

import org.deckfour.xes.model.XTrace;

import cn.edu.thu.log.read.LogBuffer;
import cn.edu.thu.log.util.Format;
import cn.edu.thu.log.util.PanGuConstants;

public class XESConvertorMonitor {

    private static XESConvertorMonitor singleton = null;

    public synchronized static XESConvertorMonitor instance() {
        if (singleton == null) {
            singleton = new XESConvertorMonitor();
        }
        return singleton;
    }
    public int numOfEvent;
    /**
     * map of laster arrival time in one case
     */
    private Hashtable<XTrace, String> lastestArrivalMap;

    /**
     * map of earliest arrival time . one case
     */
    //private Hashtable<XTrace, String> earliestArrivalMap;
    public XESConvertorMonitor() {
        this.numOfEvent = 0;
        this.lastestArrivalMap = new Hashtable<XTrace, String>();
    }

    public boolean ifExceedMaxEventNum() {
        if (numOfEvent > PanGuConstants.MAX_EVENT_NUM) {
            return true;
        }
        return false;
    }

    public void addEventNum(int addSize) {
        numOfEvent = numOfEvent + addSize;
    }

    /**
     * check if the logBuffer arriveTime is within the time limit of one case
     *
     * @param logBuffer arrived logBuffer
     * @param caseIDValue the case's caseID
     * @return true:logBuffer attive within case; false: logBuffer timeout
     */
    public boolean ifTimeOut(LogBuffer logBuffer, XTrace trace) {
        boolean ifTimeOut = false;
        String lasterTimeString = getLastestArrivalMap().get(trace);
        String arriveTimeString = logBuffer.getTimeStamp();
        Date lasterTime = Format.StringToTimeStamp(lasterTimeString);
        Date arriveTime = Format.StringToTimeStamp(arriveTimeString);
        if (arriveTime.after(lasterTime)) {
            if (Math.abs(arriveTime.getTime() - lasterTime.getTime()) > PanGuConstants.TIMEROUT) {
                ifTimeOut = true;
            }
        }
        return ifTimeOut;
    }
 /**
	 * check if the logBuffer arriveTime is within the time limit of one case
	 * 
	 * @param logBuffer
	 *            arrived logBuffer
	 * @param caseIDValue
	 *            the case's caseID
	 * @return true:logBuffer attive within case; false: logBuffer timeout
	 */
	public boolean ifTimeOut(String preTimestamp ,String currentTimestamp) {
		// Time time=new Time();
		long timeOut = PanGuConstants.TIMEROUT;
		// System.out.print("\ncheck if timeout");
		boolean ifTimeOut = false;
//                if(!getLastestArrivalMap().contains(trace)){//already remove this trace when write complete(timed out) trace to logReplace
//                    ifTimeOut=true;
//                    return ifTimeOut;
//                }
		String lasterTimeString = preTimestamp;
		//String earliestTimeString = getEarliestArrivalMap().get(trace);
		String arriveTimeString = currentTimestamp;
		Date lasterTime = Format.StringToTimeStamp(lasterTimeString);
		//Date earliestTime = Format.StringToTimeStamp(earliestTimeString);
		Date arriveTime = Format.StringToTimeStamp(arriveTimeString);
		if (arriveTime.after(lasterTime)) {
			if (Math.abs(arriveTime.getTime() - lasterTime.getTime()) > timeOut) {
				ifTimeOut = true;
	
			}
		}

		return ifTimeOut;
	}

    public void updateLastestArrivalMap(XTrace trace,
            String timeStamp) {
        getLastestArrivalMap().put(trace, timeStamp);
    }

    /**
     * @return the lastestArrivalMap
     */
    public Hashtable<XTrace, String> getLastestArrivalMap() {
        return lastestArrivalMap;
    }

    /**
     * @param lastestArrivalMap the lastestArrivalMap to set
     */
    public void setLastestArrivalMap(Hashtable<XTrace, String> lastestArrivalMap) {
        this.lastestArrivalMap = lastestArrivalMap;
    }


}
